home *** CD-ROM | disk | FTP | other *** search
/ Skunkware 5 / Skunkware 5.iso / src / Tools / vg-2.03 / jpeg / jdhuff.c < prev    next >
Encoding:
C/C++ Source or Header  |  1995-05-03  |  8.7 KB  |  460 lines

  1. /*
  2.  * Copyright (C) 1992-1993 Michael Davidson.
  3.  * All rights reserved.
  4.  *
  5.  * Permission to use, copy, modify, and distribute this software
  6.  * and its documentation for any purpose and without fee is hereby
  7.  * granted, provided that the above copyright notice appear in all
  8.  * copies and that both that copyright notice and this permission
  9.  * notice appear in supporting documentation.
  10.  *
  11.  * This software is provided "as is" without express or implied warranty.
  12.  */
  13.  
  14. /*
  15.  * This software is derived from the Independent JPEG Group's software
  16.  *
  17.  * Copyright (C) 1991, 1992 Thomas G. Lane.
  18.  * This file is part of the Independent JPEG Group's software.
  19.  *
  20.  * For conditions of distribution and use, see the accompanying README file.
  21.  */
  22.  
  23. #include "jpeg.h"
  24.  
  25. int
  26. jpegDHT(
  27.     FILE    *fp,
  28.     JPEG_TABLES    *tables
  29.     )
  30. {
  31.     int        i;
  32.     int        count;
  33.     int        length;
  34.     unsigned     code;
  35.     HUFF_TBL    *htbl;
  36.   
  37.     length    = jpegGetWord(fp) - 2;
  38.   
  39.     while (length > 0)
  40.     {
  41.     i    = jpegGetByte(fp);
  42.  
  43.     htbl    =  (i & 0x10) ? tables->ac_huff_table : tables->dc_huff_table;
  44.     i    &= 0x0f;
  45.  
  46.     if (i >= NUM_HUFF_TBLS)
  47.         return jpegError("illegal table index (%d) in DHT marker", i);
  48.  
  49.     htbl += i;
  50.  
  51.     htbl->huff_code[0].maxcode = -1;
  52.     htbl->huff_code[0].codeptr = NULL;
  53.  
  54.     count    = 0;
  55.     code    = 0;
  56.     for (i = 1; i <= 16; i++)
  57.     {
  58.         int        n;
  59.  
  60.         n        = jpegGetByte(fp);
  61.         if (n != 0)
  62.         {
  63.         htbl->huff_code[i].codeptr = & htbl->huffval[count - code];
  64.         count    += n;
  65.         code    += n;
  66.         htbl->huff_code[i].maxcode = code-1;
  67.         }
  68.         else
  69.         {
  70.         htbl->huff_code[i].maxcode = -1;
  71.         htbl->huff_code[i].codeptr = NULL;
  72.         }
  73.         code <<= 1;
  74.     }
  75.     htbl->huff_code[17].maxcode = 0xFFFFF;
  76.     htbl->huff_code[17].codeptr = NULL;
  77.  
  78.     if (count > 256)
  79.         return jpegError("more than 256 codes in DHT marker");
  80.  
  81.     for (i = 0; i < count; i++)
  82.         htbl->huffval[i] = jpegGetByte(fp);
  83.  
  84.     length -= 1 + 16 + count;
  85.     }
  86.  
  87.     if (length != 0)
  88.     return jpegError("illegal marker length in DHT marker");
  89.  
  90.     return 0;
  91. }
  92.  
  93.  
  94. int
  95. jpegDQT (
  96.     FILE    *fp,
  97.     JPEG_TABLES    *tables
  98.     )
  99. {
  100.     int        i;
  101.     int        length;
  102.     int        prec;
  103.     QUANT_VAL    *q;
  104.   
  105.     length    = jpegGetWord(fp) - 2;
  106.   
  107.     while (length > 0)
  108.     {
  109.     i    = jpegGetByte(fp);
  110.     --length;
  111.     prec    = i >> 4;
  112.     i    &= 0x0F;
  113.  
  114.     if (i >= NUM_QUANT_TBLS)
  115.         return jpegError("illegal table index (%d) in DQT marker", i);
  116.       
  117.     q    = tables->quant_table[i].q;
  118.  
  119.     if (prec)
  120.     {
  121.         for (i = 0; i < DCTSIZE2; i++)
  122.         q[i] = jpegGetWord(fp);
  123.         length -= DCTSIZE2 * 2;
  124.     }
  125.     else
  126.     {
  127.         for (i = 0; i < DCTSIZE2; i++)
  128.         q[i] = jpegGetByte(fp);
  129.         length -= DCTSIZE2;
  130.     }
  131.     }
  132.  
  133.     if (length != 0)
  134.     return jpegError("illegal marker length in DHT marker");
  135.  
  136.     return 0;
  137. }
  138.  
  139. /*
  140.  * unused bits are aligned at the most significant end of the
  141.  * buffer
  142.  */
  143. #define    DECODE_BUF_SIZE    1024
  144.  
  145. static unsigned char    decode_buf[DECODE_BUF_SIZE + 32];
  146. static unsigned long    db_offset;
  147.  
  148. #if defined(ASM)
  149.       unsigned char    *db_ptr;
  150.       unsigned char    *db_end;
  151.       unsigned long    get_buffer;    /* current bit-extraction buffer */
  152.       int        bits_left;    /* # of unused bits in it */
  153. #else
  154. STATIC unsigned char    *db_ptr;
  155. STATIC unsigned char    *db_end;
  156. STATIC unsigned long    get_buffer;    /* current bit-extraction buffer */
  157. STATIC int        bits_left;    /* # of unused bits in it */
  158. #endif
  159.  
  160. #define    MORE_BITS(fp)                    \
  161. do                            \
  162. {                            \
  163.     int    c0, c1;                        \
  164.     if ((c0 = *db_ptr++) == 0xff && (c1 = *db_ptr++) != 0)\
  165.     {                            \
  166.     if (db_ptr == db_end)                \
  167.     {                        \
  168.         fill_decode_buf(fp);            \
  169.         continue;                    \
  170.     }                        \
  171.     db_ptr -= 2;                    \
  172.     break;                        \
  173.     }                            \
  174.     bits_left += 8;                    \
  175.     get_buffer |= (c0 << (32 - bits_left));        \
  176. }  while (bits_left <= 24)
  177.  
  178. check_marker(
  179.     FILE    *fp
  180.     )
  181. {
  182.     int        c0, c1;
  183.  
  184.     if (db_ptr == db_end)    /* end of buffer */
  185.     {
  186.     fill_decode_buf(fp);
  187.     if ((c0 = *db_ptr++) == 0xff && (c1 = *db_ptr++) != 0)
  188.     {
  189.         db_ptr -= 2;
  190.         return -1;
  191.     }
  192.     return c0;
  193.     }
  194.     db_ptr -= 2;
  195.     return -1;
  196. }
  197.  
  198. /*
  199.  * fill_decode_buf()
  200.  *
  201.  * get another buffer of data for the Huffman decoder
  202.  *
  203.  * Since the Huffman decoder has to have a special check for
  204.  * 0xff escape bytes we use a special 0xff 0xff sequence to
  205.  * mark the end of the buffer. Whenever BORE_BITS() sees
  206.  * something which looks like a marker (ie 0xff followed by
  207.  * anything other than 0x00) it checks to see if it has
  208.  * reached the end of the buffer.
  209.  *
  210.  * This technique eliminates the need for an explicit check
  211.  * on the amount of data left in the buffer
  212.  */
  213. fill_decode_buf(
  214.     FILE    *fp
  215.     )
  216. {
  217.     int        nread;
  218.  
  219.     db_ptr    = decode_buf;
  220.     db_offset    = ftell(fp);
  221.     nread    = fread(decode_buf, 1, 1024, fp);
  222.  
  223. #if defined(DEBUG)
  224.     fprintf(stderr, "fill_decode_buf: offset = %6ld, nbytes = %4d",
  225.     db_offset, nread);
  226. #endif
  227.     if (nread <= 0)
  228.     {
  229.     decode_buf[0]    = 0xff;
  230.     decode_buf[1]    = 0xd9;
  231.     decode_buf[2]    = 0xff;
  232.     decode_buf[3]    = 0xff;
  233.     db_end        = &decode_buf[4];
  234.     }
  235.     else
  236.     {
  237.     db_end        = decode_buf + nread;
  238.     if (db_end[-1] == 0xff)
  239.     {
  240.         int        c;
  241.  
  242.         while ((c = getc(fp)) == 0xff)
  243.         continue;
  244.  
  245.         *db_end++ = c;
  246.     }
  247.     *db_end++    = 0xff;
  248.     *db_end++    = 0xff;
  249.     }
  250. #if defined(DEBUG)
  251.     fprintf(stderr, " db_end = %08x\n", db_end);
  252. #endif
  253. }
  254.  
  255. flush_decode_buf(
  256.     FILE    *fp
  257.     )
  258. {
  259.     long    offset;
  260.  
  261.     offset    = db_offset + (db_ptr - decode_buf);
  262.     fseek(fp, offset, 0);
  263. }
  264.  
  265. /*
  266.  * Initialize for a Huffman-compressed scan.
  267.  * This is invoked after reading the SOS marker.
  268.  */
  269.  
  270. void
  271. huff_decoder_init(
  272.     FILE    *fp
  273.     )
  274. {
  275.     /*
  276.      * Initialize static variables
  277.      */
  278.     bits_left    = 0;
  279.     get_buffer    = 0;
  280.     fill_decode_buf(fp);
  281. }
  282.  
  283. huff_decoder_term(
  284.     FILE    *fp
  285.     )
  286. {
  287.     flush_decode_buf(fp);
  288. }
  289.  
  290. /*
  291.  * Check for a restart marker & resynchronize decoder.
  292.  */
  293.  
  294. jpegCheckRestart(
  295.     FILE        *fp,
  296.     int            next
  297.     )
  298. {
  299.     int        ret = 0;
  300.   int c, nbytes;
  301.  
  302.     bits_left    = 0;
  303.     get_buffer    = 0;
  304.  
  305.     nbytes = 0;
  306.  
  307.     do
  308.     {
  309.     do
  310.     {
  311.         c = *db_ptr++;
  312.     } while (c != 0xff);
  313.  
  314.     do
  315.     {            /* skip any duplicate FFs */
  316.         if (db_ptr == db_end)
  317.         fill_decode_buf(fp);
  318.         c = *db_ptr++;
  319.     } while (c == 0xFF);
  320.     } while (c == 0);        /* repeat if it was a stuffed FF/00 */
  321.  
  322.   if (c != (RST0 + next))
  323.   {
  324.     imageWarning("Found 0x%02x marker instead of RST%d",
  325.          c, next);
  326.     ret = -1;
  327.   }
  328.  
  329.   return ret;
  330. }
  331.  
  332. int
  333. jpegDecodeMCU(
  334.     FILE        *fp,
  335.     int            MCU_blocks,
  336.     DBLOCK_INFO        MCU_info[]
  337.     )
  338. {
  339.     int            i;
  340.     int            r;
  341.     DBLOCK_INFO        *d;
  342.  
  343.     for (i = 0, d = MCU_info; i < MCU_blocks; i++, d++)
  344.     if ((r = jpegHuffDecode(fp, d->coeff_data, &d->dc_table->huff_code,
  345.         &d->ac_table->huff_code, d->dc_prediction, d->quant_table)) != 0)
  346.         return r;
  347.     return 0;
  348. }
  349.  
  350. #if !defined(ASM)
  351. /* ZAG[i] is the natural-order position of the i'th element of zigzag order.
  352.  * If the incoming data is corrupted, huff_decode_mcu could attempt to
  353.  * reference values beyond the end of the array.  To avoid a wild store,
  354.  * we put some extra zeroes after the real entries.
  355.  */
  356.  
  357. static const short ZAG[DCTSIZE2+16] = {
  358.   0,  1,  8, 16,  9,  2,  3, 10,
  359.  17, 24, 32, 25, 18, 11,  4,  5,
  360.  12, 19, 26, 33, 40, 48, 41, 34,
  361.  27, 20, 13,  6,  7, 14, 21, 28,
  362.  35, 42, 49, 56, 57, 50, 43, 36,
  363.  29, 22, 15, 23, 30, 37, 44, 51,
  364.  58, 59, 52, 45, 38, 31, 39, 46,
  365.  53, 60, 61, 54, 47, 55, 62, 63,
  366.   0,  0,  0,  0,  0,  0,  0,  0, /* extra entries in case k>63 below */
  367.   0,  0,  0,  0,  0,  0,  0,  0
  368. };
  369.  
  370. /*
  371.  * Decode a single block's worth of coefficients
  372.  */
  373. jpegHuffDecode(
  374.     FILE    *fp,
  375.     JBLOCK    *block,
  376.     HUFF_CODE    *dc,
  377.     HUFF_CODE    *ac,
  378.     JCOEF    *dcval,
  379.     QUANT_VAL     *quanttbl
  380.     )
  381. {
  382.     QUANT_VAL    q0;
  383.     int        ret_code    = 0;
  384.  
  385.     int        s, k, r;
  386.     int        code;
  387.     struct huff_code    *hc;
  388.  
  389.     /*
  390.      * Save the first entry in the quant table and set the table
  391.      * entry to 1. This allows us to fix up the DC coefficient later
  392.      */
  393.     q0        = quanttbl[0];
  394.     quanttbl[0]    = 1;
  395.  
  396.     for (k = 0, hc = dc; k < DCTSIZE2; k++, hc = ac)
  397.     {
  398.     if (bits_left < 16)
  399.         MORE_BITS(fp);
  400.  
  401.     for (code = 0; code > hc->maxcode; hc++)
  402.     {
  403.         code    += code + (get_buffer >> 31);
  404.         get_buffer    <<= 1;
  405.         --bits_left;
  406.     }
  407.  
  408.     if (hc->codeptr == NULL || bits_left < 0)
  409.     {
  410.         ret_code = -1;
  411.         break;
  412.     }
  413.  
  414.     r = hc->codeptr[code];
  415.     s = r & 15;
  416.     r = r >> 4;
  417.       
  418.     if (s)
  419.     {
  420.         register int    nbits = s;
  421.  
  422.         k += r;
  423.  
  424.         if (nbits > bits_left)
  425.         MORE_BITS(fp);
  426.  
  427.         if (get_buffer & 0x80000000)
  428.         s = get_buffer >> (32 - nbits);
  429.         else
  430.         s = (get_buffer >> (32 - nbits)) + (-1 << nbits) + 1;
  431.  
  432.         get_buffer    <<= nbits;
  433.         bits_left    -= nbits;
  434.  
  435.         /*
  436.          * Descale coefficient and output in natural order
  437.          */
  438.         (*block)[ZAG[k]] = (JCOEF) (((JCOEF) s) * quanttbl[k]);
  439.     }
  440.     else if (k != 0)
  441.     {
  442.         if (r != 15)
  443.         break;
  444.         k += 15;
  445.     }
  446.     }
  447.  
  448.     /*
  449.      * fix  quant table and DC coefficient
  450.      */
  451.     quanttbl[0]    = q0;
  452.     s        = (*block)[0];
  453.     *dcval    += s;
  454.     s        = *dcval;
  455.     (*block)[0] = (JCOEF) (((JCOEF) s) * q0);
  456.  
  457.     return ret_code;
  458. }
  459. #endif
  460.